package com.bhy702.generator.builder.util;

/**
 * @author: brbai
 * @create: 2019-11-16 12:24:52
 * @description: 字符串工具类
 */
public class StringUtil {

    /**
     * hello_world -> helloWorld
     *
     * @param camelCase
     * @return
     */
    public static String mapUnderscoreToCamelCase(String camelCase) {
        StringBuilder result = new StringBuilder();
        if (camelCase != null && camelCase.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < camelCase.length(); i++) {
                char ch = camelCase.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    /**
     * sys_user -> SysUser
     *
     * @param tableName
     * @return
     */
    public static String mapTableNameToClassName(String tableName) {
        String propertyName = mapUnderscoreToCamelCase(tableName);
        String className = (char) (propertyName.charAt(0) - 32) + propertyName.substring(1);
        return className;
    }

    /**
     * sys_user -> sysUser
     *
     * @param tableName
     * @return
     */
    public static String mapTableNameToPropertyName(String tableName) {
        String className = mapTableNameToClassName(tableName);
        String propertyName = (char) (className.charAt(0) + 32) + className.substring(1);
        return propertyName;
    }


}
